#include <iostream>

using namespace std;

typedef bool (*CompareFunc)(void* left, void* right);
typedef void (*SwapFunc)(void* left, void* right);

template<typename T>
void BubbleSort(T array[], int length, CompareFunc CFunc, SwapFunc SFunc)
{
	bool exchange = false;
	for (int i=0; i<length; ++i)
	{
		exchange = false;
		for(int j=i+1; j<length; ++j)
		{
			if( CFunc((void*)&array[j], (void*)&array[i]) )
			{
				exchange = true;
				SFunc((void*)&array[j], (void*)&array[i] );
			}
		}
		cout<<"\n The "<<i<<"th time:\n";
		for(int m=0; m<length; ++m)
		{
			array[m].show();
		}
		if(false == exchange)
			break;
	}
}

typedef struct _keya
{
	int key;
	int assist;
	void show()
	{
		cout<<key<<"	"<<assist<<endl;
	}
} KA, *PKA;

bool compare(void* left, void* right)
{
	return (((KA*)left)->key) < (((KA*)right)->key);
}

void swap(void* left, void* right)
{
	KA temp = *((KA*)left);
	*((KA*)left) = *((KA*)right);
	*((KA*)right) = temp;
}

void main()
{
	KA array[10] = {{9,90}, {8, 1}, {2,20}, {8, 3} , {5,50}, {7,70}, {6,60}, {8, 2}, {4,40}, {3,30}};
	for (int i=0; i<10; ++i)
	{
		cout<<array[i].key<<"	"<<array[i].assist<<endl;
	}

	BubbleSort(array, 10, compare, swap);

	cout<<"After BubbleSort:\n";
	for (int i=0; i<10; ++i)
	{
		cout<<array[i].key<<"	"<<array[i].assist<<endl;
	}
	cin.get();
}